﻿using System.Web.Mvc;
using System.Web.UI.WebControls;
using DotNETStack.Core.Model;
using DotNETStack.Web.Extensions;

namespace DotNETStack.Web.UI.Views.Questions
{
    public partial class Index : ViewPage
    {
        protected override void OnLoad(System.EventArgs e)
        {
            if (ViewData["Questions"] != null)
            {
                this.rpQuestions.DataSource = ViewData["Questions"];
                this.rpQuestions.DataBind();
            }
            base.OnLoad(e);
        }
        protected void rpQuestions_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;

            var dataItem = e.Item.DataItem as IQuestion;
            if (dataItem == null)
                return;

            var lnkTitle = e.Item.FindControl("lnkTitle") as HyperLink;
            if (lnkTitle != null)
            {
                lnkTitle.Text = dataItem.Title;
                lnkTitle.NavigateUrl = "~/Questions/" + dataItem.QuestionId + "/" + dataItem.Title.RemoveIllegalCharacters();
            }
        }
    }
}